package v1

import (
	"card/app/http/controllers/api"
	"card/app/logic"
	"card/app/requests"
	"card/pkg/response"
	"card/pkg/verifycode"
	"github.com/gin-gonic/gin"
)

type VerifyCodeController struct {
	api.BaseApiController
}

// ShowCaptcha 显示图片验证码
func (vc *VerifyCodeController) ShowCaptcha(c *gin.Context) {
	captchaLogic := logic.NewCaptcha(c)
	// 记录错误日志，因为验证码是用户的入口，出错时应该记 error 等级的日志
	res, err := captchaLogic.GetImage()
	if err != nil {
		response.Error(c, err)
		return
	}
	response.Data(c, res, "ok")
	return
}

// SendUsingEmail 发送 Email 验证码
func (vc *VerifyCodeController) SendUsingEmail(c *gin.Context) {
	// 1. 验证表单
	request := requests.VerifyCodeEmailRequest{}
	if err := requests.Validate(c, &request, requests.VerifyCodeEmail); err != nil {
		response.Error(c, err)
		return
	}
	// 2. 发送邮件
	err := verifycode.NewVerifyCode().SendEmail(request.Email)
	if err != nil {
		response.Warn(c, "发送 Email 验证码失败~")
		return
	} else {
		response.Success(c)
	}
}
